package cn.springsoter.core.sms.utils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class JsonUtil {
    private static ObjectMapper objectMapper;
    static {
        try {
            objectMapper = BeanUtil.getBean("objectMapper");
        } catch (NoSuchBeanDefinitionException e) {
            objectMapper = new ObjectMapper();
        }
        if(objectMapper == null) {
            objectMapper = new ObjectMapper();
        }
    }

    public static String toJson(Object sourceObject) {
        String jsonResult = null;
        try {
            jsonResult = objectMapper.writeValueAsString(sourceObject);
        } catch (Exception e) {
            log.error("", e);
            jsonResult = "{}";
        }
        return jsonResult;
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> toMap(String jsonString) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        if(!isValidJSON(jsonString)) {
            return resultMap;
        }
        try {
            resultMap = objectMapper.readValue(jsonString, Map.class);
        } catch (Exception e) {
            log.error("", e);
        }
        return resultMap;
    }

    /**
     * 校验是否合法json串
     *
     * @param json
     * @return
     */
    public static boolean isValidJSON(final String json) {
        boolean valid = false;
        try {
            if (!StringUtils.hasText(json))
                return valid;
            final JsonParser parser = objectMapper.getFactory().createParser(json);
            while (parser.nextToken() != null) {
            }
            valid = true;
        } catch (Exception e) {
        }
        return valid;
    }

    /**
     * json字符串转化成JsonNode对象
     *
     * @param json
     * @return
     */
    public static JsonNode toJsonNode(String json) {
        JsonNode jsonNode = objectMapper.createObjectNode();
        if(!isValidJSON(json)) {
            return jsonNode;
        }
        try {
            jsonNode = objectMapper.readValue(json, JsonNode.class);
        } catch (Exception e) {
            log.error("", e);
        }
        return jsonNode;
    }

    public static <T> T toObject(String jsonString, Class<T> clazz) {
        T resultObj = null;
        if(!isValidJSON(jsonString)) {
            return resultObj;
        }
        try {
            resultObj = objectMapper.readValue(jsonString, clazz);
        } catch (Exception e) {
            log.error("", e);
        }
        return resultObj;
    }

    public static <T> T toObject(String jsonString, TypeReference<T> typeReference) {
        T resultObj = null;
        if(!isValidJSON(jsonString)) {
            return resultObj;
        }
        try {
            resultObj = objectMapper.readValue(jsonString, typeReference);
        } catch (Exception e) {
            log.error("", e);
        }
        return resultObj;
    }
}
